namespace IMPlant.Core.Web.Control.GridViewFunction
{
    using IMPlant.Core.Web.Control;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomPagerSettingsFunction : ExtendFunction
    {
        public CustomPagerSettingsFunction()
        {
        }

        public CustomPagerSettingsFunction(IMPlant.Core.Web.Control.GridView grid) : base(grid)
        {
        }

        private void _grid_InitPager(object sender, GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            int dataSourceCount = pagedDataSource.DataSourceCount;
            LinkButton child = new LinkButton();
            LinkButton button2 = new LinkButton();
            LinkButton button3 = new LinkButton();
            LinkButton button4 = new LinkButton();
            TableCell cell = new TableCell();
            row.Controls.Clear();
            cell.Controls.Add(new LiteralControl("&nbsp"));
            DropDownList list = new DropDownList();
            list.Items.Add(new ListItem("每页显示5条", "5"));
            list.Items.Add(new ListItem("每页显示10条", "10"));
            list.Items.Add(new ListItem("每页显示15条", "15"));
            list.Items.Add(new ListItem("每页显示20条", "20"));
            list.Items.Add(new ListItem("每页显示25条", "25"));
            list.Items.Add(new ListItem("每页显示30条", "30"));
            for (int i = 0; i < list.Items.Count; i++)
            {
                if (list.Items[i].Value == base._grid.PageSize.ToString())
                {
                    list.SelectedValue = base._grid.PageSize.ToString();
                    break;
                }
            }
            list.TextChanged += new EventHandler(this.ddl_TextChanged);
            list.AutoPostBack = true;
            cell.Controls.Add(new LiteralControl("&nbsp"));
            string text = string.Format(base._grid.CustomPagerSettings.TextFormat, new object[] { pagedDataSource.PageSize, pagedDataSource.DataSourceCount, pagedDataSource.CurrentPageIndex + 1, pagedDataSource.PageCount });
            cell.Controls.Add(new LiteralControl(text));
            if (!string.IsNullOrEmpty(base._grid.PagerSettings.FirstPageImageUrl))
            {
                child.Text = string.Format("<img src='{0}' border='0'/>", base._grid.ResolveUrl(base._grid.PagerSettings.FirstPageImageUrl));
            }
            else
            {
                child.Text = base._grid.PagerSettings.FirstPageText;
            }
            child.CommandName = "Page";
            child.CommandArgument = "First";
            if (!string.IsNullOrEmpty(base._grid.PagerSettings.PreviousPageImageUrl))
            {
                button2.Text = string.Format("<img src='{0}' border='0'/>", base._grid.ResolveUrl(base._grid.PagerSettings.PreviousPageImageUrl));
            }
            else
            {
                button2.Text = base._grid.PagerSettings.PreviousPageText;
            }
            button2.CommandName = "Page";
            button2.CommandArgument = "Prev";
            if (!string.IsNullOrEmpty(base._grid.PagerSettings.NextPageImageUrl))
            {
                button3.Text = string.Format("<img src='{0}' border='0'/>", base._grid.ResolveUrl(base._grid.PagerSettings.NextPageImageUrl));
            }
            else
            {
                button3.Text = base._grid.PagerSettings.NextPageText;
            }
            button3.CommandName = "Page";
            button3.CommandArgument = "Next";
            if (!string.IsNullOrEmpty(base._grid.PagerSettings.LastPageImageUrl))
            {
                button4.Text = string.Format("<img src='{0}' border='0'/>", base._grid.ResolveUrl(base._grid.PagerSettings.LastPageImageUrl));
            }
            else
            {
                button4.Text = base._grid.PagerSettings.LastPageText;
            }
            button4.CommandName = "Page";
            button4.CommandArgument = "Last";
            if (base._grid.PageIndex <= 0)
            {
                child.Enabled = button2.Enabled = false;
            }
            else
            {
                child.Enabled = button2.Enabled = true;
            }
            cell.Controls.Add(child);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            cell.Controls.Add(button2);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;"));
            int num2 = base._grid.PagerSettings.PageButtonCount / 2;
            int num3 = ((base._grid.PagerSettings.PageButtonCount % 2) == 0) ? (num2 - 1) : num2;
            for (int j = 0; j < base._grid.PageCount; j++)
            {
                if ((base._grid.PageCount <= base._grid.PagerSettings.PageButtonCount) || (((j >= (base._grid.PageIndex - num3)) || (((base._grid.PageCount - 1) - j) <= (base._grid.PagerSettings.PageButtonCount - 1))) && ((j <= (base._grid.PageIndex + num2)) || (j <= (base._grid.PagerSettings.PageButtonCount - 1)))))
                {
                    if (j == base._grid.PageIndex)
                    {
                        int num8 = j + 1;
                        cell.Controls.Add(new LiteralControl("<span>" + num8.ToString() + "</span>"));
                    }
                    else
                    {
                        LinkButton button5 = new LinkButton();
                        button5.Text = (j + 1).ToString();
                        button5.CommandName = "Page";
                        button5.CommandArgument = (j + 1).ToString();
                        button5.CssClass = "";
                        cell.Controls.Add(button5);
                    }
                    cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                }
            }
            cell.Controls.Add(new LiteralControl("&nbsp;"));
            if (base._grid.PageIndex >= (base._grid.PageCount - 1))
            {
                button3.Enabled = button4.Enabled = false;
            }
            else
            {
                button3.Enabled = button4.Enabled = true;
            }
            cell.Controls.Add(button3);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            cell.Controls.Add(button4);
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            LinkButton button6 = new LinkButton();
            button6.Text = "转到";
            cell.Controls.Add(button6);
            cell.Controls.Add(new LiteralControl("第"));
            TextBox box = new TextBox();
            box.ID = "goTo";
            box.Width = Unit.Pixel(40);
            box.Height = Unit.Pixel(13);
            box.MaxLength = 6;
            box.AutoPostBack = true;
            box.Text = (base._grid.PageIndex + 1).ToString();
            box.TextChanged += new EventHandler(this.inputText_TextChanged);
            cell.Controls.Add(box);
            RangeValidator validator = new RangeValidator();
            validator.MinimumValue = "1";
            validator.MaximumValue = base._grid.PageCount.ToString();
            validator.Type = ValidationDataType.Integer;
            validator.ControlToValidate = box.ID;
            validator.ErrorMessage = "*";
            validator.Display = ValidatorDisplay.Dynamic;
            cell.Controls.Add(validator);
            cell.Controls.Add(new LiteralControl("页&nbsp;"));
            int count = base._grid.Columns.Count;
            if (count == 0)
            {
                cell.ColumnSpan = columnSpan;
            }
            else if ((count != 0) && (count < columnSpan))
            {
                cell.ColumnSpan = columnSpan;
            }
            else
            {
                cell.ColumnSpan = count;
            }
            row.Controls.Add(cell);
        }

        private void ddl_TextChanged(object sender, EventArgs e)
        {
            base._grid.PageSize = int.Parse(((DropDownList) sender).SelectedValue);
            base._grid.PageIndex = 0;
        }

        protected override void Execute()
        {
            base._grid.InitPager += new IMPlant.Core.Web.Control.GridView.InitPagerHandler(this._grid_InitPager);
        }

        private void inputText_TextChanged(object sender, EventArgs e)
        {
            TextBox box = (TextBox) sender;
            int result = 0;
            if (!int.TryParse(box.Text, out result))
            {
                result = 0;
            }
            if (result <= 0)
            {
                result = 0;
                box.Text = "1";
            }
            if (result != 0)
            {
                base._grid.PageIndex = result - 1;
            }
        }
    }
}

